#include <prototype8722alone.h>
#use rs232 (stream=midi, baud=31250, xmit=PIN_C6, rcv=PIN_C7, bits=8, stop=1, parity=n)
#use rs232 (stream=xbee, baud=9600, xmit=PIN_G1, rcv=PIN_G2)
#include <input.c>
#include <xbee_packet.c>
#include <midi.c>

char past_eventstatus;

// Still needs to ignore system exclusives
#int_rda
void serial_isr1() {
   unsigned char status, num_databytes, data1, data2;
   MIDIEvent event;
  
   init_midievent(&event);    
   status = getc();
  
   if (status > 0x7F) {
      num_databytes = lookup_mididatabytes(status);
      event = get_midievent(status, num_databytes);
      output_midievent(event);
      past_eventstatus = status;
   }
   else if((past_eventstatus > 0x7F) && (past_eventstatus < 0xF0)) {
      num_databytes = lookup_mididatabytes(past_eventstatus) - 1;
      data1 = status;
     
      if (num_databytes > 0){
         data2 = getc();
      }
     
      status = past_eventstatus;
      set_midievent(&event, status, data1, data2);
      output_midievent(event);
   }
}

#int_rda2
void serial_isr2() {
   BYTE b;
   MIDIEvent event;
  
   b = getc(xbee);
   if(b == 0x01) {   //Volume Change
      b = getc(xbee);
      set_midievent(&event, 0xB0, 0x07, b);
      output_midievent(event);
   }else if(b == 0x02) {  //Pitch Wheel Change
      b = getc(xbee);
      //set_midievent(&event, 0xE0, b, 0x00);
      set_midievent(&event, 0xE0, 0x00, b);
      output_midievent(event);
   } else if (b == 0x03) {  //Wah-Wah Effect
      b = getc(xbee);
      //set_midievent(&event, 0xF4, b, 0x00);
      set_midievent(&event, 0xF4, 0x00, b);
      output_midievent(event);
   }
}

void main() {  
   LED_OFF(GREEN_LED);
   LED_OFF(RED_LED);
   LED_OFF(YELLOW_LED);
  
   enable_interrupts(int_rda);
   enable_interrupts(int_rda2);
  
   while(TRUE) {
      enable_interrupts(global);
   }
}
